//
//  LoginViewController.m
//  VICKY ZONE
//
//  Created by sunx on 15/3/19.
//  Copyright (c) 2015年 sunx. All rights reserved.
//

#import "LoginViewController.h"
#import "IQKeyboardManager.h"
#import "IQKeyboardReturnKeyHandler.h"
#import "IQUIView+IQKeyboardToolbar.h"
#import "SVProgressHUD.h"
#import "MainViewController.h"

#define SERVE @"http://www.samoyun.com/controller/loginController.php"

@interface LoginViewController ()
@property(nonatomic,strong) NSDictionary *infoDictionary;
@property(nonatomic,strong) NSUserDefaults *userDefault;
@property(nonatomic) int integer;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.userDefault = [NSUserDefaults standardUserDefaults];
    
    self.infoDictionary = [[NSBundle mainBundle] infoDictionary];
    
    NSString *app_Version = [self.infoDictionary objectForKey:@"CFBundleShortVersionString"];
    /*
        if debug
     */
    //[self.userDefault setInteger:0 forKey:app_Version];
    //[self.userDefault setObject:@"" forKey:@"username"];

    
    int status = (int)[self.userDefault integerForKey:app_Version];
    
    if(status == 0) {
        [self showGuidePage];
    }
}

- (void)viewDidAppear:(BOOL)animated {
    self.username = (NSString *)[self.userDefault valueForKey:@"username"];
    //NSLog(@"wiew will appear :%@",self.username);
    self.userField.text = @"";
    self.pwdField.text = @"";
    if(self.username.length != 0) {
        [self jumpToNext];
    }
}


#pragma mark - login action

-(IBAction)login:(id)sender {
    [SVProgressHUD show];
    [self post];
}
- (void)post {
    NSString *password = [MD5Tool md5:self.pwdField.text];
    NSString *email = self.userField.text;
    [NSURLSession POST:SERVE pararms:@{@"email" : email,@"pwd" : password} completed:^(NSData *data) {
        if(data != nil) {
            
            NSString *username = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            
            [self.userDefault setObject:username forKey:@"username"];
            
            self.username = username;
            
            NSLog(@"post back is :%@;",self.username);
            if(username.length >=2){
                [self performSelectorOnMainThread:@selector(jumpToNext) withObject:self waitUntilDone:YES];
            }else {
                [self performSelectorOnMainThread:@selector(noUserError) withObject:self waitUntilDone:YES];
            }
        }
    } failed:^(NSError *error){
        NSLog(@"%@",error);
    }];
}

- (void)jumpToNext {
    UINavigationController *navigation = [[UINavigationController alloc] init];
    MainViewController *mainViewController = [[MainViewController alloc] init];
    [navigation pushViewController:mainViewController animated:YES];
    [self presentViewController:navigation animated:YES completion:^{
        NSLog(@"jump complete...");
    }];
}
- (void)noUserError {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ERROR" message:@"用户名密码错误" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alert show];
    [SVProgressHUD dismiss];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - guide page
- (void)showGuidePage {
    EAIntroPage *page1 = [EAIntroPage page];
    page1.bgImage = [UIImage imageNamed:@"1.jpg"];
    
    EAIntroPage *page2 = [EAIntroPage page];
    page2.bgImage = [UIImage imageNamed:@"2.jpg"];
    
    EAIntroPage *page3 = [EAIntroPage page];
    page3.bgImage = [UIImage imageNamed:@"3.jpg"];
    
    EAIntroPage *page4 = [EAIntroPage page];
    page4.bgImage = [UIImage imageNamed:@"4.jpg"];
    
    EAIntroView *intro = [[EAIntroView alloc] initWithFrame:self.view.bounds andPages:@[page1,page2,page3,page4]];
    
    [intro setDelegate:self];
    [intro showInView:self.view animateDuration:0.0];
    
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    
    NSString *app_Version = [self.infoDictionary objectForKey:@"CFBundleShortVersionString"];
    
    [userDefault setInteger:1 forKey:app_Version];
}
- (void)viewWillDisappear:(BOOL)animated {
    [SVProgressHUD dismiss];
}
- (void)introDidFinish {
    NSLog(@"guide finish...");
}
- (IBAction)backToHere:(UIStoryboardSegue *)sender {
}
@end
